package com.hx.test.domain;

import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "t_user")
@Data
@Accessors(chain = true)
@NamedEntityGraph(name = "User.Graph",attributeNodes = {@NamedAttributeNode(value = "roles",subgraph = "Resource.Graph")},
        subgraphs = {@NamedSubgraph(name = "Resource.Graph",attributeNodes = {@NamedAttributeNode(value = "resources")})})
public class User {

    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(name = "id")
    private String id;

    @Column(name = "name")
    private String name;

    //级联操作(删除user,删除对应的中间表一行数据,或role也被删除???需要测试)
    @ManyToMany(cascade = CascadeType.ALL)
//    @ManyToMany(mappedBy = "users",cascade = CascadeType.ALL)
    @JoinTable(name = "t_user_role",joinColumns =@JoinColumn(name = "user_id"),inverseJoinColumns = @JoinColumn(name = "role_id"))
    private List<Role> roles;


}
